package com.jxwifi.cloud.quickclean.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.idroid.widget.LoadDialog;
import com.jxwifi.cloud.quickclean.MainActivity;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.app.ClientBasicActivity;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 绑定手机号（用于微信登录）
 */
public class BindPhoneActivity extends ClientBasicActivity implements BindPhoneView {

    @Bind(R.id.et_login_bind_phone)
    EditText mEtLoginBindPhone;
    @Bind(R.id.et_login_bind_verification)
    EditText mEtLoginBindVerification;
    @Bind(R.id.tv_bind_obtain_verification)
    TextView mTvBindObtainVerification;
    @Bind(R.id.tv_bind_button)
    TextView mTvBindButton;

    private BindPhonePresenter mPresenter;
    private boolean mIsSelection = true;

    private LoadDialog mLoadDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_phone);
        ButterKnife.bind(this);
        openEventBus();
        mLoadDialog = new LoadDialog(mBaseContext);
        mPresenter=new BindPhonePresenter(mBaseContext,this);
        initView();
    }

    public void initView(){
        mPresenter.ChangedPhone();
        mPresenter.ChangedVerification();
    }

    @OnClick({R.id.tv_bind_obtain_verification,R.id.tv_bind_button})
    void clickCK(View v){
        switch (v.getId()){
            case R.id.tv_bind_obtain_verification:
                mPresenter.validation();
                break;

            case R.id.tv_bind_button:
                mLoadDialog.show();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPresenter.judge();
                        mLoadDialog.dismiss();
                    }
                }, 1000);
                break;
        }
    }

    @Override
    public void onSuccessIntent() {
        Intent intent = new Intent();
        intent.setClass(mBaseContext, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public TextView getVer() {
        return mTvBindObtainVerification;
    }

    @Override
    public TextView getLoginButton() {
        return mTvBindButton;
    }

    @Override
    public EditText getBindPhone() {
        return mEtLoginBindPhone;
    }

    @Override
    public EditText getBindVerification() {
        return mEtLoginBindVerification;
    }
    @Override
    public boolean getIsSelection() {
        return mIsSelection;
    }

    @Override
    public void onFailure(int errorCode, String msg) {
        Toast toast = Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
    }
}
